package com.example.h5server.controller;

import com.example.h5server.model.UploadFile;
import com.example.h5server.service.IUploadFileService;
import com.example.h5server.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 纸鸽
 * @since 2025-11-03
 */
@RestController
@RequestMapping("/uploadFile")
@Tag(name = "上传文件路径接口")
public class UploadFileController {
    @Autowired
    private IUploadFileService uploadFileService;

    @GetMapping("/getUploadFilePath")
    @Operation(summary = "获取上传文件路径")
    public Result<List<UploadFile>> getUploadFilePath() {
        try {
            List<UploadFile> uploadFiles = uploadFileService.list();
            return Result.success("查询成功", uploadFiles);
        } catch (Exception e) {
            return Result.error("查询失败"+e.getMessage());
        }
    }
    @PostMapping("/update")
    @Operation(summary = "修改上传文件路径信息")
    public Result<UploadFile> updateUploadFilePath(@RequestBody UploadFile uploadFile) {
        try {
            boolean updated = uploadFileService.updateById(uploadFile);
            if (updated) {
                return Result.success("数据更新成功",uploadFile);
            } else {
                return Result.error("数据更新失败");
            }
        } catch (Exception e) {
            return Result.error("数据更新失败"+e.getMessage());
        }
    }
    @PostMapping("/add")
    @Operation(summary = "新增上传文件路径信息")
    public Result<UploadFile> addUploadFilePath(@RequestBody UploadFile uploadFile) {
        try {
            boolean result = uploadFileService.save(uploadFile);
            return result ? Result.success("添加成功",uploadFile) : Result.error("添加失败");
        } catch (Exception e) {
            return Result.error("添加失败"+e.getMessage());
        }
    }


    @GetMapping("/getFilePath")
    @Operation(summary = "根据id查询上传文件路径信息")
    public Result<UploadFile> getUploadFilePathById(@RequestParam("id") Long id) {
        try {
            UploadFile uploadFile = uploadFileService.getById(id);
            if (uploadFile != null) {
                return Result.success("查询成功", uploadFile);
            } else {
                return Result.error("上传文件路径信息不存在");
            }
        } catch (Exception e) {
            return Result.error("查询失败"+e.getMessage());
        }
    }
}
